//  Inside Microsoft Exchange 2007 Web Services 
//  Chapter 10
//
//  Copyright (c) 2007 David Sterling, Ben Spain, Mark Taylor, Huw Upshall, Michael Mainer.  
//  All rights reserved.
//
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using ProxyHelpers.EWS;

namespace GetMeetingRequestFromItemId
{
    class Program
    {
        static void Main(string[] args)
        {
            ExchangeServiceBinding binding = new ExchangeServiceBinding();
            binding.Url = @"https://yourServer/ews/Exchange.asmx";
            binding.Credentials = new NetworkCredential(
                "username",
                "password",
                "domain");

            // TODO: You must have an ItemId of an existing meeting request in order to call
            // GetMeetingRequestFromItemId
        }

        /// <summary>
        /// Gets a MeetingRequestMessageType instance from the id of a 
        /// meeting request. (Listing 10-16)
        /// </summary>
        /// <param name="binding">Binding to use for the web service call</param>
        /// <param name="meetingRequestItemId">id of the meeting request</param>
        /// <returns>Meeting request as a MeetingRequestMessageType instance</returns>
        public static MeetingRequestMessageType GetMeetingRequestFromItemId(
            ExchangeServiceBinding binding,
            ItemIdType meetingRequestItemId)
        {
            // Create a GetItem request for the default properties of an already 
            // known meeting request
            //
            GetItemType getItemRequest = new GetItemType();
            getItemRequest.ItemIds = new ItemIdType[] { meetingRequestItemId };
            getItemRequest.ItemShape = new ItemResponseShapeType();
            getItemRequest.ItemShape.BaseShape = DefaultShapeNamesType.Default;

            // Call the GetItem web method via the Exchange Serivce Binding
            GetItemResponseType getItemResponse =
                binding.GetItem(getItemRequest);

            // Verify that the GetItem request was successful
            if (getItemResponse.ResponseMessages.Items[0].ResponseClass !=
                ResponseClassType.Success)
            {
                // Indicate that we have a problem
                throw new Exception(String.Format(
                    "Unable to get the meeting request\r\n{0}\r\n{1}",
                    getItemResponse.ResponseMessages.Items[0].ResponseCode,
                    getItemResponse.ResponseMessages.Items[0].MessageText));
            }

            // Verify that the item is indeed a meeting request
            ItemInfoResponseMessageType itemInfoResponseMessage =
                (ItemInfoResponseMessageType)getItemResponse.
                    ResponseMessages.Items[0];

            if (itemInfoResponseMessage.Items.Items[0] is MeetingRequestMessageType)
            {
                // Return the id of the meeting request
                return (MeetingRequestMessageType)itemInfoResponseMessage.
                    Items.Items[0];
            }
            else
            {
                throw new Exception("The id supplied was not for a meeting request.");
            }
        }

    }
}
